using UnityEngine;
using QFramework;

// 1.请在菜单 编辑器扩展/Namespace Settings 里设置命名空间
// 2.命名空间更改后，生成代码之后，需要把逻辑代码文件（非 Designer）的命名空间手动更改
namespace QFramework.Example
{
	public partial class Knifes : BulletFathers,IController
	{
        public Vector2 dics;
        public float moveSpeed;
        public EnemyFathers enemyFathers;
        private Rigidbody2D rigidbody;
        public float maxFlyTime;
        public override float MaxBulletFlyTime => maxFlyTime;

        public Transform startPos;
        public override Transform StartPos => startPos;

        public override BoxCollider2D boxCollider => SelfBoxCollider2D;

        private int hotIndex = 0;
        public override int HotIndex => hotIndex;

        private void Awake()
        {
            rigidbody = this.GetComponent<Rigidbody2D>();

            var AttackCount = 0;
            SelfBoxCollider2D.OnTriggerEnter2DEvent(collider => 
            {
                //Debug.Log(collider.name);

                var hurtbox = collider.GetComponent<HitHurtBox>();
                if (hurtbox != null)
                {
                    if (hurtbox.Owner.CompareTag("Enemy"))
                    {

                        var enemyfather = hurtbox.Owner.GetComponent<EnemyFathers>();

                        if (enemyfather is IEnemy ienemy)
                        {
                            
                            DamageSystem.SetDamage(DataManager.Instance.simpleKnifeModel.AbilityDamage.Value, enemyfather);
                            //enemyfather.Hurt(simpleKnifeModel.SimpleKnifeDamage);

                            //AttackCount++;
                            //if (AttackCount >= DataManager.Instance.abilityUpModel.AbilitysItemCount.Value)
                            //{
                                //Debug.Log("穿透次数耗尽,进行伤害计算");

                                this.DestroyGameObjGracefully();
                            //}

                        }

                        //hurtbox.Owon.GetComponent<IEnemy>().Hurt(simpleKnifeModel.SimpleKnifeDamage);

                    }
                }


            }).UnRegisterWhenGameObjectDestroyed(gameObject);
        }

        void Start()
		{
			// Code Here
		}

        public override void BulletMoveDic(Vector2 dic)
        {
            base.BulletMoveDic(dic);
            ///dics = dic;
        }

        public override void Dis()
        {
            base.Dis();
        }
        public override void SetBulletScale(float scale)
        {
            base.SetBulletScale(scale);
            this.transform.localScale = new Vector3(scale, scale, scale);
        }

        public override void SetBulletSpeed(float speed)
        {
            base.SetBulletSpeed(speed);
            moveSpeed = speed;
        }

        public void SetEnemyFather(EnemyFathers enemy)
        {
            enemyFathers = enemy;
            //if (enemyFathers!=null)
            //{
            //    Debug.Log("设置enemy成功");
            //}
        }

        public IArchitecture GetArchitecture()
        {
            return Global.Interface;
        }

        private void Update()
        {

            Atk();
        }

        void Atk()
        {
            if (enemyFathers != null)
            {
                var dic = enemyFathers.NormalizedDirection2DFrom(Player.Instance);
                this.transform.up = dic;

                rigidbody.velocity = enemyFathers.NormalizedDirection2DFrom(Player.Instance) * 5;

                if (Player.Instance)
                {
                    if (Player.Instance.Distance2D(this) >10)
                    {
                        Destroy(this.gameObject);
                    }
                }
            }
            else
            {
                Destroy(this.gameObject);
            }
            
        }
    }
}
